from charm.toolbox.pairinggroup import PairingGroup, G1, G2, GT, ZR, pair as e
from Util import *


class Group(object):
    __curve_params = {
        'a': '/home/lh/python_project/security-lab/pbc.conf.d/a.properties',
        'a1': '/home/lh/python_project/security-lab/pbc.conf.d/a1.properties',
        'a159': '/home/lh/python_project/security-lab/pbc.conf.d/a159.properties',
        'a201': '/home/lh/python_project/security-lab/pbc.conf.d/a201.properties',
        'a224': '/home/lh/python_project/security-lab/pbc.conf.d/a224.properties',
        'e': '/home/lh/python_project/security-lab/pbc.conf.d/e.properties',
        'f': '/home/lh/python_project/security-lab/pbc.conf.d/f.properties',
        'g149': '/home/lh/python_project/security-lab/pbc.conf.d/a149.properties',
    }

    def __init__(self, curve_type = 'a'):
        if curve_type in self.__curve_params:
            try:
                self.group = PairingGroup(self.__curve_params[curve_type], param_file = True)
            except e:
                print('pairing group initialize failed, please change the field \'__curve_params\' in file Group.py ')
                raise e
        else:
            raise ValueError(f'invalid curve type {curve_type}, ')

    def from_hash(self, b: bytes, g_type = G1, hash_algorithm = sha256str):
        return self.group.hash(hash_algorithm(b), g_type)
